package com.ease.framework.gef4p.commands {
   import com.ease.framework.gef.IEditor;
   import com.ease.framework.gef4p.editparts.EdgeEditPart;
   import com.ease.framework.gef4p.editparts.NodeEditPart;
   import com.ease.framework.gef4p.model.Edge;
   import com.ease.framework.gef4p.model.Node;
   import com.ease.framework.workbench.Workbench;
   import com.ease.framework.workbench.spi.impls.Command;

   public class MoveEdgeTargetAnchorCommand extends Command {
      private var originalTargetNode:Node;
      private var currentTargetNode:Node;
      private var nowEdge:Edge;

      private var tranEP:EdgeEditPart;


      public function MoveEdgeTargetAnchorCommand(edge:Edge, node:Node) {
         super();
         nowEdge = edge;
         originalTargetNode = nowEdge.target;
         currentTargetNode = node;
      }

      override public function execute():void {
         if (tranEP == null) {
            tranEP = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(nowEdge) as EdgeEditPart;
         }
         tranEP.getTarget().removeInEdge(tranEP);
         nowEdge.target = currentTargetNode;
         var targetEP:NodeEditPart = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(currentTargetNode) as NodeEditPart;
         tranEP.setTarget(targetEP);
         tranEP.refreashFigure();
      }

      override public function undo():void {
         tranEP.getTarget().removeInEdge(tranEP);
         nowEdge.target = originalTargetNode;
         var targetEP:NodeEditPart = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(originalTargetNode) as NodeEditPart;
         tranEP.setTarget(targetEP);
         tranEP.refreashFigure();
      }
   }
}